#Load the W-NOMINATE package
library(wnominate)
#
#Set up a dummy to orient the spatial dimensions.  As we do not rely on the 
#visualizations, we can pick anything we like.
#
tmpl=c(1,1,1,1,1,1,1,1,1,1)
#
#We now run W-NOMINATE over all ten dimensions
for(k in 1:10){
tmp2<-paste("results_",k,"d",sep="")
#
#Create the W-NOINATE results directory if it does not exist
dir.create(tmp2)
#
#Run through the specified data.  Here we look at House 113 for each of the 10 holdout #samples
#
for(i in 113) {
for(j in 1:10) {
  infile <- paste("./holdout samples/house-",i,"-holdout-",j,".csv",sep="")
  outfile1 <- paste("./results_",k,"d/house-",i,"-",j,"-leg-",k,"d.csv",sep="")
  outfile2 <- paste("./results_",k,"d/house-",i,"-",j,"-rollcall-",k,"d.csv",sep="")
tryCatch({
tmp<-read.csv(infile,header=FALSE,strip.white=TRUE)
names<-tmp[,1]
tmp<-tmp[,-c(1)]
rc<-rollcall(tmp,yea=c(1,2,3),nay=c(5,6,7),missing=c(9),notInLegis =0, legis.names=names)
result<-wnominate(rc,polarity=tmpl[1:k],dims=k)
#
#Write results into two files
#
write.table(result$legislators,file=outfile1,sep=",")
write.table(result$rollcalls,file=outfile2,sep=",")
},error = function(e) {tmp3<-paste("Error: (",k,i,j,")") 
print(tmp3)})
}}}
#
#This script is based on the script in Poole, Keith, Jeffrey Lewis, James Lo, and Royce 
#Carroll. 2011. �Scaling Roll Call Votes with wnominate in R.� Journal of Statistical Software 42(14): 1�21
